﻿using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.Security;
using Medianamik.Core.Sugar;
using Roles = Medianamik.Core.Security.Roles;

namespace Medianamik.UI.Web.ModalWindows.ContentManagement
{
    public partial class TypeSelectorWindow : BasePage
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            _limitToParentTypeId = Request.QueryString.GetValue<Guid?>(DefaultParamNames.LimitToParentTypeId);
            _parentNodeID = Request.QueryString.GetValue<Guid?>(DefaultParamNames.ParentNodeID);

            var childTypes = GetLegitimateChildTypes();

            if (childTypes.Count > 0)
            {
                ddlNodeTypes.DataSource = childTypes.OrderBy(t => t.LocalizedName);
                ddlNodeTypes.DataBind();

                if (AddDefaultValue) ddlNodeTypes.Items.Insert(0, "");

                if (SelectedValue != null) ddlNodeTypes.SelectByValue(SelectedValue);
            }
            else
            {
                ShowError("No type to create");
            }
        }

        protected virtual IList<NodeType> GetLegitimateChildTypes()
        {
            var parents = (ParentNode == null ? NodeTypeManager.GetLegitimateChildTypes(ParentNodeId) : ParentNode.LegitimateChildTypes)
                .Where(t => Roles.UserHasRight(t, SecurityLevels.Create) && !t.IsSystem && !t.Listable);

            if (LimitToParentTypeId.HasValue)
            {
                var limitIdNode = NodeTypeManager.GetNodeType(LimitToParentTypeId.Value);
                parents = parents.Where(limitIdNode.IsParentOf).OrderBy(t => t.LocalizedName);
            }

            if ((ParentNode != null && ParentNode.WebSite != null) || ConfigManager.GetAllWebSites(CurrentEditionCulture).Count < 1)
                return parents.Where(t => !t.ID.Equals(MedianamikTypes.WebSiteProxy)).ToList();

            return parents.ToList();
        }

        Guid? _parentNodeID;
        protected Guid? ParentNodeId
        {
            get { return _parentNodeID; }
        }

        private Node _parentNode;
        protected Node ParentNode
        {
            get
            {
                return _parentNode ??
                    (_parentNode = (!ParentNodeId.HasValue || ParentNodeId.Value == Guid.Empty ? default(Node) :
                    NodeManager.GetNode(ParentNodeId.Value)));
            }
        }

        private Guid? _limitToParentTypeId;
        protected Guid? LimitToParentTypeId
        {
            get { return _limitToParentTypeId; }
        }

        private void ShowError(string error)
        {
            phrMain.Visible = false;
            phrError.Visible = true;
            litError.Text = error;
        }

        protected virtual bool AddDefaultValue
        {
            get { return true; }
        }

        protected virtual string SelectedValue
        {
            get { return null; }
        }
    }
}
